package com.spider.silence.main;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spider.silence.downloader.SinaNewsDownloader;
import com.spider.silence.downloader.SinaSiChuanDownLoader;
import com.spider.silence.pool.SinaSiChuanDownLoaderPool;
import com.spider.silence.utils.PropertiesUtil;
import com.spider.silence.worker.SinaSiChuanWorker;
import org.apache.log4j.PropertyConfigurator;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Linbo Hu on 2017/6/20.
 * 新浪四川爬虫程序主入口
 */
public class SinaSiChuanMain {

    public static void main(String[] args) throws IOException, InterruptedException {
        //初始化日志环境
        PropertyConfigurator.configure(PropertiesUtil.getLogPro());
        //初始化下载池
        SinaSiChuanDownLoaderPool sinaSiChuanDownLoaderPool = SinaSiChuanDownLoaderPool.getInstance();
        //获取一个下载器
        SinaSiChuanDownLoader sinaSiChuanDownLoader = sinaSiChuanDownLoaderPool.fetchDownLoader(1000);
        //生成爬虫任务
        sinaSiChuanDownLoader.generateTask();
        //将这个下载器放入下载池
        sinaSiChuanDownLoaderPool.releaseDownLoader(sinaSiChuanDownLoader);
        //new Thread(new SinaSiChuanWorker()).start();
        ExecutorService pool =  Executors.newFixedThreadPool(3000);
        //初始化新浪四川的Worker并且添加到线程池中
        for(int i = 0; i<Integer.valueOf(PropertiesUtil.getVal("SinaSiChuan.workCount")); i++){
            pool.execute(new SinaSiChuanWorker());
        }

        /*SinaSiChuanDownLoader sinaSiChuanDownLoader1 = new SinaSiChuanDownLoader();
        while (true) {
            try {
                JSONObject object = sinaSiChuanDownLoader1.crawlItem("http://sc.sina.com.cn/news/c/2017-05-24/detail-ifyfkqwe0982045.shtml");
                System.out.println(object.toJSONString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*/
    }

}
